package com.ruoyi.seckill.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class SeckillDataInitializer implements CommandLineRunner {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        String stockKey = "seckill:stock:1001";

        if (!Boolean.TRUE.equals(redisTemplate.hasKey(stockKey))) {
            redisTemplate.opsForValue().set(stockKey, "10000");
            System.out.println("Redis 秒杀库存初始化成功");
        } else {
            System.out.println("Redis 库存已存在，跳过初始化");
        }
    }
}
